package com.coin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.Market;
import com.coin.dto.*;
import com.coin.feign.CoinServiceFeign;
import com.coin.mapper.MarketMapper;
import com.coin.service.MarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 李力
 * @description 针对表【market(交易对配置信息)】的数据库操作Service实现
 * @createDate 2025-06-20 14:51:04
 */
@Service
public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market>
        implements MarketService {

    @Autowired
    private CoinServiceFeign coinServiceFeign;

    /**
     * @Description: 交易市场的分页查询
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @Override
    public Page<Market> findByPage(Page<Market> page, Long tradeAreaId, Byte status) {
        LambdaQueryWrapper<Market> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(tradeAreaId != null, Market::getTradeAreaId, tradeAreaId)
                .eq(status != null, Market::getStatus, status);
        return page(page, queryWrapper);
    }

    /**
     * @Description: 启用/禁用交易市场
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setStatus(Market market) {
        if (market == null) {
            throw new RuntimeException("参数为空");
        }
        return updateById(market);
    }

    /**
     * @Description: 新增一个市场
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveMarket(Market market) {
        if (market == null) {
            throw new RuntimeException("参数为空");
        }
        Long sellCoinId = market.getSellCoinId(); // /报价货币
        Long buyCoinId = market.getBuyCoinId(); // 基础货币
        if (sellCoinId == null || buyCoinId == null) {
            throw new RuntimeException("参数为空");
        }
        List<CoinDto> coins = coinServiceFeign.findCoins(Arrays.asList(sellCoinId, buyCoinId));
        if (coins == null || coins.isEmpty()) {
            throw new RuntimeException("货币输入错误");
        }
        CoinDto coinDto = coins.get(0);
        CoinDto sellCoin = null;
        CoinDto buyCoin = null;
        if (coinDto.getId().equals(sellCoinId)) {
            sellCoin = coinDto;
            buyCoin = coins.get(1);
        } else {
            sellCoin = coins.get(1);
            buyCoin = coinDto;
        }
        market.setName(sellCoin.getName() + "/" + buyCoin.getName()); // 交易市场的名称  报价货币/基础货币
        market.setTitle(sellCoin.getTitle() + "/" + buyCoin.getTitle()); // 交易市场的标题 报价货币/基础货币
        market.setSymbol(sellCoin.getName() + buyCoin.getName()); // 交易市场的标识 报价货币基础货币
        market.setImg(sellCoin.getImg()); // 交易市场的图标
        return save(market);
    }

    /**
     * @Description: 修改一个市场
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateMarket(Market market) {
        if (market == null) {
            throw new RuntimeException();
        }
        return updateById(market);
    }

    @Override
    public List<Market> listMarket() {
        return list();
    }

    @Override
    public List<Market> getMarkets(Long id) {
        if (id == null) {
            throw new RuntimeException("无交易区域");
        }
        LambdaQueryWrapper<Market> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(Market::getTradeAreaId, id)
                .eq(Market::getStatus, 1)
                .orderByAsc(Market::getSort);
        return list(queryWrapper);
    }

    @Override
    public Market getMarkerBySymbol(String symbol) {
        if (StrUtil.isEmpty(symbol)) {
            throw new RuntimeException("参数为空");
        }
        LambdaQueryWrapper<Market> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(StrUtil.isNotEmpty(symbol), Market::getSymbol, symbol);
        return getOne(queryWrapper);
    }

    @Override
    public MarketDto findByCoinId(Long buyCoinId, Long sellCoinId) {
        LambdaQueryWrapper<Market> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(buyCoinId != null, Market::getBuyCoinId, buyCoinId)
                .eq(sellCoinId != null, Market::getSellCoinId, sellCoinId);
        Market market = getOne(queryWrapper);
        if (market == null) {
            return null;
        }
        MarketDto marketDto = new MarketDto();
        BeanUtil.copyProperties(market, marketDto);
        return marketDto;
    }
}




